package com.bj58.mis.apm.server.statistics.service.impl;

import com.bj58.mis.apm.server.core.common.result.Page;
import com.bj58.mis.apm.server.core.common.result.SimplePage;
import com.bj58.mis.apm.server.statistics.dto.chart.ChartData;
import com.bj58.mis.apm.server.statistics.dto.chart.ValueBean;
import com.bj58.mis.apm.server.statistics.dto.job.KylinJob;
import com.bj58.mis.apm.server.statistics.dto.job.KylinJobInfo;
import com.bj58.mis.apm.server.statistics.dto.job.KylinJobStepInfo;
import com.bj58.mis.apm.server.statistics.mapper.JobMapper;
import com.bj58.mis.apm.server.statistics.query.PageInfo;
import com.bj58.mis.apm.server.statistics.query.job.JobQuery;
import com.bj58.mis.apm.server.statistics.service.BaseService;
import com.bj58.mis.apm.server.statistics.service.JobService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 〈一句话功能简述〉<br> 
 * JobService实现类
 *
 * @author zhangyu84
 * @create 2018/7/2
 * @since 1.0.0
 */
@Service
@Transactional
public class JobServiceImpl extends BaseService<JobMapper> implements JobService {
    @Override
    public KylinJob jobSurvey(@NotNull String currUser, @NotNull JobQuery jobQuery) {
        KylinJob kylinJob = new KylinJob();
        boolean isAdmin = this.isAdmin(currUser);
        List<String> cubeLists = null;
        if (!isAdmin && StringUtils.isEmpty(jobQuery.getCubeId())) {
            cubeLists = this.getAuthCubeIdList(currUser,jobQuery.getProjectId());
            if (cubeLists == null || cubeLists.size() == 0) {
                return kylinJob;
            }
        }
        kylinJob = this.mapper.jobSurvey(isAdmin, cubeLists, jobQuery);
        List<KylinJobInfo> kylinJobInfos = this.mapper.jobInfosSurvey(isAdmin, cubeLists, jobQuery);
        if (kylinJobInfos.size() > 0) {
            ChartData chartData = new ChartData();
            List<String> category = new ArrayList<>(kylinJobInfos.size());
            List<ValueBean> valueBeans=new ArrayList<>(1);
            ValueBean valueBean = new ValueBean();
            List<Double> dates=new  ArrayList<>(kylinJobInfos.size());
            for (int i = 0; i < kylinJobInfos.size(); i++) {
                category.add(kylinJobInfos.get(i).getJobName());
                dates.add(kylinJobInfos.get(i).getJobTime());
            }
            valueBean.setData(dates);
            valueBeans.add(valueBean);
            chartData.setCategory(category);
            chartData.setValueBeans(valueBeans);
            kylinJob.setKylinJobInfos(chartData);
            chartData.setOriginalDatas(kylinJobInfos);
        }
        return kylinJob;
    }

    @Override
    public Page<KylinJobInfo> jobList(@NotNull String currUser, @NotNull JobQuery jobQuery, @NotNull PageInfo pageInfo) {
        boolean isAdmin =false;
        //搜索负责人 默认走数据库
        if(!StringUtils.isEmpty(jobQuery.getOwner())){
            currUser=jobQuery.getOwner();
        }else{
            isAdmin=this.isAdmin(currUser);
        }

        List<String> cubeLists = null;
        if (!isAdmin && StringUtils.isEmpty(jobQuery.getCubeId())) {
            cubeLists = this.getAuthCubeIdList(currUser,jobQuery.getProjectId());
            if (cubeLists == null || cubeLists.size()==0) {
                return new SimplePage<KylinJobInfo>(0,Collections.emptyList(),pageInfo.getPage(),pageInfo.getLimit());
            }
        }
        List<KylinJobInfo> kylinJobInfos=this.mapper.jobList(isAdmin,cubeLists,jobQuery,pageInfo);
        return new SimplePage<KylinJobInfo>(this.mapper.getJobNumber(isAdmin,cubeLists,jobQuery),kylinJobInfos,pageInfo.getPage(),pageInfo.getLimit());
    }

    @Override
    public KylinJobStepInfo getJobInfoById(@NotNull String id) {
        KylinJobStepInfo kylinJobStepInfo=new KylinJobStepInfo();
        kylinJobStepInfo.setKylinJobInfo(this.mapper.kylinJobInfo(id));
        kylinJobStepInfo.setKylinJobSteps(this.mapper.kylinJobStepsByJobId(id));
        return kylinJobStepInfo;
    }
}
